package com.example.shop_2211.fragment

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.shop_2211.R
import com.example.shop_2211.adapter.GoodsAdapter
import com.example.shop_2211.adapter.GoodsMutiAdapter
import com.example.shop_2211.adapter.MenuAdapter
import com.example.shop_2211.adapter.MoneyAdapter
import com.example.shop_2211.databinding.FragmentHomeBinding
import com.example.shop_2211.detail.DetailActivity
import com.example.shop_2211.fgViewModel.HomeViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    val imgs = mutableListOf<String>()//存放轮播图图片网址
    lateinit var menuAdapter: MenuAdapter//定义适配器
    lateinit var moneyAdapter: MoneyAdapter
    lateinit var goodsAdapter: GoodsMutiAdapter
    var type = 4//分类
    var page = 1//页面
    override fun initView() {
        //初始化rvMenu
        menuAdapter = MenuAdapter()
        binding.rvMenu.adapter = menuAdapter
        binding.rvMenu.layoutManager = GridLayoutManager(context,5)


        moneyAdapter = MoneyAdapter()
        binding.rvMoney.adapter = moneyAdapter
        binding.rvMoney.layoutManager = GridLayoutManager(context,4)

        viewModel.index()

        goodsAdapter = GoodsMutiAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)

        viewModel.indexGoods(type,5,page)
        //刷新加载更多
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                viewModel.indexGoods(type,5,page)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page+=1
                viewModel.indexGoods(type,5,page)
                binding.srl.finishLoadMore()

            }
        })
        //点击分类，商品内容进行切换
        moneyAdapter.setOnItemClickListener { adapter, view, position ->
            moneyAdapter.setType(moneyAdapter.data[position].type.toInt())
            type = moneyAdapter.data[position].type.toInt()
            page = 1//每次切换数据时，页面重置为1
            viewModel.indexGoods(type,5,page)
        }
        //跳转到搜索页面
        binding.tvSearch.setOnClickListener {
            ARouter.getInstance().build("/app/SearchNewActivity").navigation()
        }
        //点击跳转到详情页
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
     /*       val intent = Intent(context, DetailActivity::class.java)
            intent.putExtra("goodsId",goodsAdapter.data[position].id)
            startActivity(intent)*/
            ARouter.getInstance().build("/app/DetailActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }

    }

    override fun initData() {
        viewModel.indexSuccess.observe(this){
            if(it.code==200){
                //轮播图
                it.data.banner.forEach {
                    imgs.add(it.pic)
                }
                binding.banner.setImages(imgs)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null && imageView != null) {
                               Glide.with(context).load(path).into(imageView)
                            }
                        }
                    })
                binding.banner.setDelayTime(2000)//轮播时长
                binding.banner.setBannerStyle(BannerConfig.NUM_INDICATOR)//设置轮播指示器
                binding.banner.setOnBannerListener {
                    ToastUtils.showLong("点击了第$it 张图片")
                }
                binding.banner.start()
                //菜单
                menuAdapter.data.clear()
                menuAdapter.data.addAll(it.data.menus)
                menuAdapter.notifyDataSetChanged()
                //分类
                moneyAdapter.data.clear()
                moneyAdapter.data.addAll(it.data.explosiveMoney)
                moneyAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.indexFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.indexGoodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
                if(it.data.list.size==0){
                    ToastUtils.showLong("没有更多数据了")
                }
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.indexGoodsFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<HomeViewModel>  = HomeViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_home

}